package server.business.task.data;

import java.util.Calendar;

import server.ServerConfigCache;
import server.business.task.config.TaskConfig;
import server.commonData.User;
import server.commonData.db.TaskDb;
import server.commonData.interfaces.IUpdate;
import server.msgBean.LoginMessage.TaskInfo;

/**
 * 任务对象
 * @author King
 *
 */
public class Task
{
	private int taskId;

	/**是否领取奖励过**/
	private boolean isAward;
	
	/**当前的进度**/
	private int jindu;
	
	/**是否可以领奖**/
	private boolean canAward;
	
	/**上次刷新日期**/
	private int lastRefreshDay;
	
	public Task(int taskId)
	{
		this.taskId = taskId;
	}
	
	public int getTaskId() {
		return taskId;
	}

	/**清除数据**/
	public void clear()
	{
		if(isAward)
			return;
		this.jindu = 0;
		this.isAward = false;
		this.canAward = false;
	}
	
	public TaskConfig getConfig()
	{
		return ServerConfigCache.taskMap.get(this.taskId);
	}

	public boolean isAward() {
		return isAward;
	}

	public void setAward(boolean isAward) {
		this.isAward = isAward;
	}

	public int getJindu() {
		return jindu;
	}

	public void setJindu(int jindu) {
		this.jindu = jindu;
	}

	public boolean isCanAward() {
		return canAward;
	}

	public void setCanAward(boolean canAward) {
		this.canAward = canAward;
	}
	
	public TaskInfo.Builder showToClient(User user)
	{
		TaskConfig config = getConfig();
		TaskInfo.Builder builder = TaskInfo.newBuilder();
		builder.setTaskId(this.taskId);
		builder.setPar(getPar(user));
		builder.setCanAward(this.canAward);
		
		builder.setConditionDes(config.conditionDes);
		builder.setAwardDes(config.awardDes);
		builder.setAwardMoney(config.awardMoney);
		builder.setConditionType(config.conditionType);
		if(config.conditionPar!=null)
			builder.setConditionPar(config.conditionPar);
		
		return builder;
	}
	
	/**获得打牌对应的参数**/
	private int getPar(User user)
	{
		TaskConfig config = getConfig();
		switch (config.conditionType) {
		case TaskEnum.CHONGZHI:
			return user.getRechargedGold();
		case TaskEnum.PLAYER_NUM:
			return user.getFailNum()+user.getWinNum();
		case TaskEnum.LEVEL:
			return user.getLevel();
		case TaskEnum.VIPLEVEL:
			return user.getVipLevel();
		case TaskEnum.JINBI:
			return user.getMoney();
		default:
			return 1;
		}
	}
	
	public TaskDb toDb()
	{
		TaskDb db = new TaskDb();
		db.taskId = taskId;
		db.canAward = canAward;
		db.isAward = isAward;
		db.jindu = jindu;
		db.lastRefreshDay = lastRefreshDay;
		return db;
	}
	
	public void fromDb(TaskDb db)
	{
		this.taskId = db.taskId;
		this.canAward = db.canAward;
		this.isAward = db.isAward;
		this.jindu = db.jindu;
		this.lastRefreshDay = db.lastRefreshDay;
	}

	public int getLastRefreshDay() {
		return lastRefreshDay;
	}

	public void setLastRefreshDay(int lastRefreshDay) {
		this.lastRefreshDay = lastRefreshDay;
	}
	
}
